package service

import (
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"strings"
	"tuowei.com/app/admin/models"
)

type OrderService struct {
	service.Service
}

// GetAllCourseIds 获取学生购买的所有课程所包含的题库ids或者试卷id
// buyerId 学员id
// giftType 类型，question_bank练习题，试卷examination
func (o *OrderService) GetAllCourseIds(buyerId int, giftType string) []string {
	/// 获取线上订单
	onlineLists := make([]models.OnlineOrder, 0)
	offlineLists := make([]models.OfflineOrder, 0)
	o.Orm.Where("buyer_id = ?", buyerId).Where("status >= 2").Preload("Course").Select("id").Find(&onlineLists)
	o.Orm.Where("buyer_id = ?", buyerId).Where("status = 4").Preload("Course").Select("id").Find(&offlineLists)

	offlineCourseIds := make([]int, 0)
	onlineCourseIds := make([]int, 0)
	for _, v := range onlineLists {
		if len(v.Course) > 0 {
			for _, vv := range v.Course {
				onlineCourseIds = append(onlineCourseIds, vv.CourseId)
			}
		}
	}
	for _, v := range offlineLists {
		if len(v.Course) > 0 {
			for _, vv := range v.Course {
				offlineCourseIds = append(offlineCourseIds, vv.CourseId)
			}
		}
	}
	// 获取课程相关的绑定的题库id
	giftLists := make([]models.SysCourseGift, 0)
	o.Orm.Where("gift_type = ? and course_type = 'online' and 	course_id in (?)", giftType, onlineCourseIds).
		Or("gift_type = ? and course_type = 'offline' and 	course_id in (?)", giftType, offlineCourseIds).Find(&giftLists)

	arr := make([]string, 0)
	for _, v := range giftLists {
		if v.ObjIds != "" {
			arr = append(arr, strings.Split(v.ObjIds, ",")...)
		}
	}
	return arr
}
